﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;

enum State
{
    closed,
    opening,
    open,
    closing,

}

public class FanAnimator : MonoBehaviour
{

    Image img;
    [SerializeField] Sprite[] sprt;

    [SerializeField] string prefix;
    [SerializeField] int startIndex;
    [SerializeField] int endIndex;

    [SerializeField] int currentIndex;

    float frameTime = 0.02f;
    float timeElapsed = 0;

    State state = State.closed;

    [SerializeField] Sprite s;
    void Start ()
    {

        //animator = GetComponent<Animator> ();
        img = GetComponent<Image> ();

        sprt = Resources.LoadAll<Sprite> ("Fan");
    }

    void Update ()
    {
        timeElapsed += Time.deltaTime;

        switch (state)
        {
            case State.closed:
                break;
            case State.closing:
                {
                    if (timeElapsed > frameTime)
                    {
                        timeElapsed = 0;
                        if (currentIndex > startIndex)
                            currentIndex--;
                        else
                            state = State.closed;
                    }
                }
                break;
            case State.open:
                break;
            case State.opening:
                {
                    if (timeElapsed > frameTime)
                    {
                        timeElapsed = 0;
                        if (currentIndex < endIndex)
                            currentIndex++;
                        else
                            state = State.open; //Has already been at open
                    }
                };
                break;
        }

        img.sprite = sprt[currentIndex - startIndex];
    }

    public void Open ()
    {
        state = State.opening;
    }

    public void Close ()
    {
        state = State.closing;
    }
}